package com.itheima.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.domain.HomeNewProduct;
import com.itheima.mapper.HomeNewProductMapper;
import com.itheima.service.IHomeNewProductService;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * <p>
 * 新鲜好物表 服务实现类
 * </p>
 *
 * @author fukeke
 * @since 2022-11-11
 */
@DubboService
public class HomeNewProductServiceImpl extends ServiceImpl<HomeNewProductMapper, HomeNewProduct> implements IHomeNewProductService {

    @Autowired
    HomeNewProductMapper homeNewProductMapper;

    @Override
    public PageBeanVo findlistByPage(Integer pageNum, Integer pageSize, String productName, Integer recommendStatus) {
        LambdaQueryWrapper<HomeNewProduct>lqw=new LambdaQueryWrapper<>();
        Page<HomeNewProduct>page=new Page<>(pageNum,pageSize);

        lqw.like(productName!=null,HomeNewProduct::getProductName, productName);



            lqw.eq(recommendStatus!=null,HomeNewProduct::getRecommendStatus,recommendStatus);
            lqw.orderByDesc(HomeNewProduct::getSort);


             homeNewProductMapper.selectPage(page, lqw);

            return new PageBeanVo(pageNum,pageSize,page.getTotal(),page.getRecords());



    }

    @Override
    public void addNew(HomeNewProduct homeNewProduct) {
        homeNewProductMapper.insert(homeNewProduct);

    }

    @Override
    public void delete(Integer[] ids) {
        if(ArrayUtil.isNotEmpty(ids))
        {
            for (Integer id : ids) {
                homeNewProductMapper.deleteById(id);

            }

        }
    }

    @Override
    public void update(Integer[] ids, Integer recommendStatus) {
        if(ArrayUtil.isNotEmpty(ids))
        {
            for (Integer id : ids) {
                HomeNewProduct homeNewProduct = homeNewProductMapper.selectById(id);
                homeNewProduct.setRecommendStatus(recommendStatus);
                homeNewProductMapper.updateById(homeNewProduct);

            }
        }
    }

    @Override
    public void sort(Integer id, Integer sort) {
        HomeNewProduct homeNewProduct = homeNewProductMapper.selectById(id);
        homeNewProduct.setSort(sort);
        homeNewProductMapper.updateById(homeNewProduct);
    }
}
